<?php
/**
 * Object Relational Persistence Mapping Class for table: lab_assignment_ticket
 * @package modules.dept_laboratory.ordo
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class LabAssignmentTicket extends ORDataObject {

	/**#@+
	 * Fields of table: lab_assignment_ticket mapped to class members
	 */
	var $lab_assignment_ticket_id		= '';
	var $ticket_id		= '';
	var $treatment_doctor_id		= '';
	var $treatment_department_id		= '';
	var $enum_status		= '';
	var $request_time		= '';
	var $return_date		= '';
	var $patient_id		= '';
	var $diagnosis		= '';
	var $enum_patient_type		= '';

	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'lab_assignment_ticket';

	/**
	 * Primary Key
	 */
	var $_key = 'lab_assignment_ticket_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'LabAssignmentTicket';

	/**
	 * Handle instantiation
	 */
	function __construct() {
		parent::ORDataObject();
	}

	public function setData($labAssignmentTicket) {
		if (is_array($labAssignmentTicket)) {
			$this->populateArray($labAssignmentTicket);
		}
	}
	/**
	 * List lab assignment of givent ticketId.
	 * @param $ticketId
	 * @return array[n](lab_assignment_ticket_id, request_time, diagnosis, PE.name treatment_doctor_name, treatment_doctor_id, enum_status)
	 */
	public function listAssignments($ticketId) {
		$ds = new Datasource_sql();
		$sql = array(
            'cols'  => "lab_assignment_ticket_id, request_time, diagnosis, PE.name treatment_doctor_name, treatment_doctor_id, enum_status",
            'from'  => "$this->_table t LEFT JOIN person PE ON (t.treatment_doctor_id > 0 AND t.treatment_doctor_id = PE.person_id)", 
            'where' => "ticket_id = '" . $ticketId ."'",
            'orderby' => 'request_time DESC'
        
            );
            $ds->setup($this->_db, $sql);
            $result = $ds->toArray();
            return $result;
	}

	/**
	 * List open lab assignment of givent patientId.
	 * @param $ticketId
	 * @return array[n](lab_assignment_ticket_id, request_time, diagnosis, PE.name treatment_doctor_name, treatment_doctor_id, enum_status)
	 */
	public function listAssignmentsByPId($patientId, $enumStatusArray = array()) {
		 $statusCondition = 'AND enum_status IN (';
        foreach ($enumStatusArray as $enum) {
             $status = Utils::getEnumKey(EnumerationConstant::LAB_ASSIGNMENT_TICKET_STATUS, $enum);
             $statusCondition .= $status . ",";
            
        }        
        $statusCondition .= '0)';
		$ds = new Datasource_sql();
		$sql = array(
            'cols'  => "lab_assignment_ticket_id, request_time, diagnosis, PE.name treatment_doctor_name, treatment_doctor_id, enum_status",
            'from'  => "$this->_table t LEFT JOIN person PE ON (t.treatment_doctor_id > 0 AND t.treatment_doctor_id = PE.person_id)", 
            'where' => "patient_id = '" . $patientId . "'" . $statusCondition,
            'orderby' => 'request_time DESC'
        
            );
            $ds->setup($this->_db, $sql);
            $result = $ds->toArray();
            return $result;
	}
    /**
     * List open lab assignment of givent ticketId.
     * @param $ticketId
     * @return array[n](lab_assignment_ticket_id, request_time, diagnosis, PE.name treatment_doctor_name, treatment_doctor_id, enum_status)
     */
    public function listAssignmentsByTicketId($ticketId, $enumStatusArray = array()) {
        $statusCondition = 'AND enum_status IN (';
        foreach ($enumStatusArray as $enum) {
             $status = Utils::getEnumKey(EnumerationConstant::LAB_ASSIGNMENT_TICKET_STATUS, $enum);
             $statusCondition .= $status . ",";
        	
        }        
        $statusCondition .= '0)';
        
        $ds = new Datasource_sql();
        $sql = array(
            'cols'  => "lab_assignment_ticket_id, request_time, diagnosis, PE.name treatment_doctor_name, treatment_doctor_id, enum_status",
            'from'  => "$this->_table t LEFT JOIN person PE ON (t.treatment_doctor_id > 0 AND t.treatment_doctor_id = PE.person_id)", 
            'where' => "ticket_id = '" . $ticketId . "'" . $statusCondition,
            'orderby' => 'request_time DESC'
        
            );
            $ds->setup($this->_db, $sql);
            $result = $ds->toArray();
            return $result;
    }

	public function getAssignmentInfo($labAssignmentTicket) {
		$field =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
		$ds = new Datasource_sql();
		$sql = array(
            'cols'  => "lab_assignment_ticket_id, request_time, diagnosis, PE.name treatment_doctor_name, 
                        treatment_doctor_id, enum_status, patient_id, 
                        d.$field treatment_department_name",
            'from'  => "$this->_table t LEFT JOIN person PE ON (t.treatment_doctor_id > 0 AND t.treatment_doctor_id = PE.person_id)
                        LEFT JOIN department d ON (d.department_id = t.treatment_department_id)
                        ", 
            'where' => "lab_assignment_ticket_id = '" . $labAssignmentTicket ."'",
		);
		
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) {
			return null;
		}
		return $result[0];
	}


	public function getEnumStatus() {
		return $this->enum_status;
	}
	/**
	 * Delete a givent labAssignmentTicketId.
	 * @param $labAssignmentTicketId
	 * @return key of deleted row
	 */
	public function deleleLabAssignment($labAssignmentTicketId) {
		$sql = "DELETE FROM $this->_table WHERE lab_assignment_ticket_id='$labAssignmentTicketId'";
		$this->_execute($sql);
		return $labAssignmentTicketId;
	}

	public function getEnumPatientType() {
		return $this->enum_patient_type;
	}
	public function getTicketId() {
		return $this->ticket_id;
	}
	public function getLabAssignmentTicketId() {
		return $this->lab_assignment_ticket_id;
	}
	public function getTreatmentDoctorId() {
		return $this->treatment_doctor_id;

	}
	public function getDiagnosis() {
		return $this->diagnosis;
	}
	public function getRequestTime() {
		return $this->request_time;
	}
	public function setEnumStatus($enumStatus) {
		$status = Utils::getEnumKey(EnumerationConstant::LAB_ASSIGNMENT_TICKET_STATUS, $enumStatus);
		$this->enum_status = $status;
	}
	
	

	/*
	 *
	 */
	public function calculatePrices($labAssignmentTicketId) {
		// Insurance type of the ticket		
		$ds = new Datasource_sql();
		$sql = array(
            'cols'  => "SUM(original_price) original_price, SUM(insurance_price) insurance_price, SUM(patient_price) patient_price",
            'from'  => "$this->_table t 
                        INNER JOIN lab_ticket lt ON (t.lab_assignment_ticket_id=lt.lab_assignment_ticket_id AND is_done='1')
                        INNER JOIN lab_assigned_item lai ON(lt.lab_ticket_id=lai.lab_ticket_id AND lai.is_done='1')   ", 
            'where' => "t.lab_assignment_ticket_id = '" . $this->lab_assignment_ticket_id ."'",

		);
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) {
			return array('original_price'=>0, 'insurance_price'=>0, 'patient_price'=>0);
		}
		if ($result[0]['original_price'] == null) {
			$result[0]['original_price'] = 0;
		}
		if ($result[0]['insurance_price'] == null) {
			$result[0]['insurance_price'] = 0;
		}
		if ($result[0]['patient_price'] == null) {
			$result[0]['patient_price'] = 0;
		}
		return $result[0];

	}





}
?>
